package com.lucy.nepu.module.book.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.core.BaseEntity;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.nepu.module.book.dao.BookDAO;
import com.lucy.nepu.module.book.entity.Book;
import com.lucy.nepu.module.book.event.BookCreatedEvent;
import com.lucy.nepu.module.book.event.BookDeletedEvent;
import com.lucy.nepu.module.book.service.BookService;
import com.lucy.nepu.module.htopic.entity.HorTopic;
import com.lucy.nepu.module.htopic.event.HorTopicCreatedEvent;
import com.lucy.nepu.module.htopic.event.HorTopicDeletedEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookDAO dao;

    @Resource
    ApplicationContext context;

    @Override
    public Book getOneUnwrap(String s) {
        Optional<Book> bookOp = dao.findById(s);
        if (!bookOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return bookOp.get();
    }

    @Override
    public PageResultDTO<Book> getAll(Pageable pageable, Example<Book> example) {
        Page<Book> page;

        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }
        else {
            page = dao.findAll(example,pageable);
        }
        /*
          获取分页主体内容
         */
        List<Book> list = page.getContent();
        PageResultDTO<Book> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<Book> getExample(Example<Book> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        Book entity = getOneUnwrap(id);

        dao.deleteById(id);
        // 事件发布
        context.publishEvent(new BookDeletedEvent(this,entity));
    }

    @Override
    public Optional<Book> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public Book saveOne(Book o) {
        if (Validator.isNotEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"新建DTO不可携带ID");
        }
        Book saveRes= dao.save(o);
        // 事件发布
        context.publishEvent(new BookCreatedEvent(this,saveRes));
        return saveRes;
    }

    @Override
    public Book updateOne(Book o) {
        if (Validator.isEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"更新DTO必须携带ID");
        }
        return dao.save(o);
    }
}
